package com.zhuiyi.yiqingbackend.service.impl;

import com.zhuiyi.yiqingbackend.common.ErrorCode;
import com.zhuiyi.yiqingbackend.domain.Base;
import com.zhuiyi.yiqingbackend.domain.Car;
import com.zhuiyi.yiqingbackend.domain.Cure;
import com.zhuiyi.yiqingbackend.domain.Patient;
import com.zhuiyi.yiqingbackend.domain.request.AddConfirmPatientRequest;
import com.zhuiyi.yiqingbackend.domain.request.AddPatientRequest;
import com.zhuiyi.yiqingbackend.domain.request.PatientUpdateStatusRequest;
import com.zhuiyi.yiqingbackend.domain.response.PatientResponse;
import com.zhuiyi.yiqingbackend.exception.BusinessException;
import com.zhuiyi.yiqingbackend.mapper.BaseMapper;
import com.zhuiyi.yiqingbackend.mapper.PatientMapper;
import com.zhuiyi.yiqingbackend.service.BaseService;
import com.zhuiyi.yiqingbackend.service.CarService;
import com.zhuiyi.yiqingbackend.service.CureService;
import com.zhuiyi.yiqingbackend.service.PatientService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PatientServiceImpl implements PatientService {
    @Resource
    private PatientMapper patientMapper;
    @Resource
    private BaseService baseService;
    @Resource
    private CarService carService;
    @Resource
    private CureService cureService;
    @Resource
    private BaseMapper baseMapper;

    @Override
    public List<PatientResponse> getPatients() {
        List<Patient> patients = patientMapper.getPatients();
        List<PatientResponse> list = new ArrayList<>();
        for (int i = 0; i < patients.size(); i++) {
            int baseid = patients.get(i).getBaseid();
            Base base = baseService.getBaseById(baseid);
            Car car = carService.getCarByBaseId(baseid);
            Cure cure = cureService.getCureByBaseId(baseid);
            list.add(new PatientResponse(patients.get(i), car, base,cure));
        }
        return list;
    }

    @Override
    public boolean addPatient(AddPatientRequest request) {
        String phone = request.getPhone();
        int baseid = baseService.getBaseByPhone(phone).getId();
        Patient patient = new Patient(request, baseid);   // 创建一个新的Patient对象 包含baseid
        boolean result = patientMapper.addPatient(patient);
        if(!result) {
            throw new BusinessException(ErrorCode.INSERT_FAIL);
        }
        return result;
    }

    @Override
    public boolean addPatient2(AddConfirmPatientRequest request) {
        String hospital = request.getHospital();
        String idCard = request.getIdCard();
        String phone = request.getPhone();
        Date onSetTime = request.getOnSetTime();
        String source = request.getSource();
        String symbol = request.getSymbol();
        String userName = request.getUserName();

        if(StringUtils.isBlank(phone)||StringUtils.isEmpty(phone)){
            throw new BusinessException(ErrorCode.PARAM_ERROR,"手机号为空");
        }
//学到了，当前的service可以直接调用别的 service 和 mapper！！！！！
        Base base = baseMapper.getBaseByPhone(phone);
        if(base==null){
            baseMapper.addBase(new Base(userName,idCard,phone));
            Base addBase = baseService.getBaseByPhone(phone);
            int baseId = addBase.getId();
            boolean result = patientMapper.addPatient(new Patient(hospital, onSetTime, source, symbol, baseId));
            return result;
        }else {
            int baseId = base.getId();
            return patientMapper.addPatient(new Patient(hospital, onSetTime, source, symbol, baseId));
        }

    }

    @Override
    public boolean deletePatientById(int id) {
        boolean result = patientMapper.deletePatient(id);
        if(!result) {
            throw new BusinessException(ErrorCode.DELETE_FAIL);
        }
        return result;
    }

    @Override
    public PatientResponse getPatientById(int id) {
        Patient patient = patientMapper.getPatientById(id);
//        if(patient == null) {
//            throw new BusinessException(ErrorCode.NO_DATA,"没有给病人");
//        }
        int baseid = patient.getBaseid();
        Base base = baseService.getBaseById(baseid);
        Car car = carService.getCarByBaseId(baseid);
        Cure cure = cureService.getCureByBaseId(baseid);

        return new PatientResponse(patient, car, base,cure);
    }

    @Override
    public boolean updatePatient(Patient patient) {
        boolean result = patientMapper.updatePatient(patient);
        if(!result) {
            throw new BusinessException(ErrorCode.UPDATE_FAIL);
        }
        return result;
    }

    @Override
    public Patient getPatientByBaseID(int baseid) {
        return patientMapper.getPatientByBaseId(baseid);
    }

    @Override
    public boolean updateStatus(PatientUpdateStatusRequest request) {
        int id = request.getId();
        int status = request.getStatus();
//        System.out.println(status);
        Patient patient = patientMapper.getPatientById(id);
        if(patient==null){
            throw new BusinessException(ErrorCode.NO_DATA,"更新失败，没有该病人");
        }
        patient.setStatus(status);
        boolean result = patientMapper.updatePatient(patient);
        return result;
    }
}
